package com.rchat.pocmini.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.adapter.PushMessageOffLineAdapter;
import com.rchat.pocmini.activity.bean.PushMessageOffLineBean;
import com.rchat.pocmini.bean.JPushBean;
import com.rchat.pocmini.common.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ljd on 2017/4/11.
 */

public class PushMessageOffLineActivity extends BaseActivity implements View.OnClickListener {
    public static final String KEY_BUNDLE_JPUSHMESSAGE = "push_off_line_json";
    public static final String KEY_BUNDLE_JPUSHMESSAGE_OBJ = "JPUSHMESSAGEOBJ";
    private ImageView ivBack;
    private ListView lvPushMessageOffLine;
    private PushMessageOffLineAdapter adapter;
    private List<PushMessageOffLineBean> dataList;
    private JPushBean.DataBean mDataBean;

    @Override
    public int getLayoutId() {
        return R.layout.push_message_off_line_activity_layout;
    }

    private void initData(JPushBean.DataBean dataBean) {
        dataList = new ArrayList<>();
        parseBean(dataBean);
        adapter = new PushMessageOffLineAdapter(this, dataList);
        lvPushMessageOffLine.setAdapter(adapter);
    }

    private void parseBean(JPushBean.DataBean bean) {
        for (JPushBean.DataBean.ApplyGroupsBean applyGroupsBean : bean.getApplyGroups()) {
            PushMessageOffLineBean pushMessageOffLineBean = new PushMessageOffLineBean();
            pushMessageOffLineBean.setGroupName(applyGroupsBean.getGroupName());
            pushMessageOffLineBean.setGroupId(applyGroupsBean.getGroupId());
            pushMessageOffLineBean.setUserName(applyGroupsBean.getUserName());
            pushMessageOffLineBean.setDisplayName(applyGroupsBean.getDisplayName());
            pushMessageOffLineBean.setCreateTime(applyGroupsBean.getCreateTime());
            pushMessageOffLineBean.setType(PushMessageOffLineBean.APPLY_GROUP);
            dataList.add(pushMessageOffLineBean);
        }
        for (JPushBean.DataBean.ApplyFriendBean applyFriendBean : bean.getApplyFriend()) {
            PushMessageOffLineBean pushMessageOffLineBean = new PushMessageOffLineBean();
            pushMessageOffLineBean.setUserName(applyFriendBean.getUserName());
            pushMessageOffLineBean.setDisplayName(applyFriendBean.getDisplayName());
            pushMessageOffLineBean.setCreateTime(applyFriendBean.getCreateTime());
            pushMessageOffLineBean.setType(PushMessageOffLineBean.APPLY_FRIEND);
            dataList.add(pushMessageOffLineBean);
        }

        for (JPushBean.DataBean.ReplyGroupBean replyGroupBean : bean.getReplyGroup()) {
            PushMessageOffLineBean pushMessageOffLineBean = new PushMessageOffLineBean();
            pushMessageOffLineBean.setMessage(replyGroupBean.getMessage());
            pushMessageOffLineBean.setCreateTime(replyGroupBean.getCreateTime());
            pushMessageOffLineBean.setType(PushMessageOffLineBean.REPLY_GROUP);
            dataList.add(pushMessageOffLineBean);
        }

        for (JPushBean.DataBean.ReplyFriendBean replyFriendBean : bean.getReplyFriend()) {
            PushMessageOffLineBean pushMessageOffLineBean = new PushMessageOffLineBean();
            pushMessageOffLineBean.setMessage(replyFriendBean.getMessage());
            pushMessageOffLineBean.setCreateTime(replyFriendBean.getCreateTime());
            pushMessageOffLineBean.setType(PushMessageOffLineBean.REPLY_FRIEND);
            dataList.add(pushMessageOffLineBean);
            dataList.add(pushMessageOffLineBean);
        }
        for (JPushBean.DataBean.InviteMembersBean inviteMembersBean : bean.getInviteMembers()) {
            PushMessageOffLineBean pushMessageOffLineBean = new PushMessageOffLineBean();
            pushMessageOffLineBean.setMessage(inviteMembersBean.getMessage());
            pushMessageOffLineBean.setCreateTime(inviteMembersBean.getCreateTime());
            pushMessageOffLineBean.setType(PushMessageOffLineBean.INVITE_MEMBER);
            dataList.add(pushMessageOffLineBean);
        }

    }


    @Override
    public void initView() {
        lvPushMessageOffLine = (ListView) findViewById(R.id.list_view_push_message_off_line);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        mDataBean = (JPushBean.DataBean) getIntent().getSerializableExtra(KEY_BUNDLE_JPUSHMESSAGE_OBJ);
        initData(mDataBean);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
        }
    }
}
